package com.gnerv.cms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gnerv.cms.common.Result;
import com.gnerv.cms.dto.DictDTO;
import com.gnerv.cms.dto.DictSaveDTO;
import com.gnerv.cms.dto.DictSearchDTO;
import com.gnerv.cms.service.IDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 基础平台-字典 controller 类
 *
 * @author zhang
 * @date 2019/12/2
 **/
@Api(tags = "字典管理")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/manage/dict")
public class DictController {

    @Resource
    private IDictService dictService;

    @RequestMapping(value = "save", method = RequestMethod.POST)
    public Result saveDict(@RequestBody DictSaveDTO dictSaveDTO) {
        dictService.saveDict(dictSaveDTO);
        return Result.response();
    }

    @RequestMapping(value = "create", method = RequestMethod.POST)
    public Result createDict(@RequestBody DictSaveDTO dictSaveDTO) {
        dictService.createDict(dictSaveDTO);
        return Result.response();
    }

    @RequestMapping(value = "delete/{dictId}", method = RequestMethod.GET)
    public Result deleteByDictId(@PathVariable(value = "dictId") String dictId) {
        dictService.deleteDict(dictId);
        return Result.response();
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public Result updateDict(@RequestBody DictSaveDTO dictSaveDTO) {
        dictService.updateDict(dictSaveDTO);
        return Result.response();
    }

    @RequestMapping(value = "/{dictId}/status/{status}", method = RequestMethod.GET)
    public Result updateDictStatus(@PathVariable(value = "dictId") String dictId, @PathVariable(value = "status") Integer status) {
        dictService.updateDictStatus(dictId, status);
        return Result.response();
    }

    @RequestMapping(value = "select/{dictId}", method = RequestMethod.GET)
    public Result selectByDictCode(@PathVariable(value = "dictId") String dictId) {
        DictDTO dict = dictService.selectDict(dictId);
        return Result.response().data("dict", dict);
    }

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public Result listDict(DictSearchDTO dictSearchDTO,
                           @RequestParam(value = "current", defaultValue = "1") Integer current,
                           @RequestParam(value = "size", defaultValue = "30") Integer size) {
        Page<DictDTO> page = dictService.listDict(dictSearchDTO, current, size);
        return Result.response().data("dicts", page);
    }


    @RequestMapping(value = "map", method = RequestMethod.GET)
    public Result mapDict() {
        Map<String, DictDTO> dictMap = dictService.mapDict();
        return Result.response().data("dictMap", dictMap);
    }

}
